package vip.xiaonuo.hontu.listener.handle.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.stereotype.Service;
import vip.xiaonuo.hontu.listener.handle.HandleService;
import vip.xiaonuo.hontu.listener.handle.model.HongTuPushData;
import vip.xiaonuo.hontu.listener.handle.model.TransPushData;
import vip.xiaonuo.modular.htpushdata.entity.HtPushData;
import vip.xiaonuo.modular.htpushdata.service.HtPushDataService;
import vip.xiaonuo.modular.transdata.entity.TransData;
import vip.xiaonuo.modular.transdata.service.TransDataService;

import javax.annotation.Resource;

@Service
public class TransPushDataHandleService implements HandleService<TransPushData> {

    @Resource
    private HtPushDataService pushDataService;

    @Resource
    private TransDataService transDataService;

    @Override
    public void handle(HtPushData data) {
        try{
            String content = data.getValue();
            HongTuPushData pushData = JSON.parseObject(content,HongTuPushData.class);
            TransPushData securityData = JSON.parseObject(pushData.getData(), TransPushData.class);
            if(data.getStatus()==0){
                this.saveData(securityData);
            }
            data.setStatus(1);
        }catch (Exception e){
            data.setFailReason(ExceptionUtils.getMessage(e));
            data.setStatus(2);
        }finally {
            pushDataService.saveOrUpdate(data);
        }


    }

    @Override
    public void saveData(TransPushData data) {
        TransData transData = new TransData();
        BeanUtil.copyProperties(data,transData);

//        if(ObjectUtil.isNotNull(transData.getPersonImageUrl()) && transData.getPersonImageUrl().startsWith("http") ){
//            transData.setPersonImageUrl( getImageBase64(transData.getPersonImageUrl())  );
//        }
//        if(ObjectUtil.isNotNull(transData.getSnapshotUrl()) && transData.getSnapshotUrl().startsWith("http") ){
//            transData.setSnapshotUrl( getImageBase64(transData.getSnapshotUrl())  );
//        }
//        if(ObjectUtil.isNotNull(transData.getFrameUrl()) && transData.getFrameUrl().startsWith("http") ){
//            transData.setFrameUrl( getImageBase64(transData.getFrameUrl())  );
//        }
        transData.setPushFlag(false);
        transDataService.save(transData);
    }
}
